package com.atguigu.juc.tool;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierTest {

    private static int[][] dataArray;

    static {
        dataArray = new int[3][5];
        dataArray[0] = new int[]{0,0,0,1,2};
        dataArray[1] = new int[]{0,0,1,2,2};
        dataArray[2] = new int[]{0,1,2,2,2};
    }

    public static void main(String[] args) {

        CyclicBarrier cyclicBarrier = new CyclicBarrier(3);

        for (int i = 0; i < 3; i++) {

            int index = i;

            new Thread(()->{
                try {
                    int[] array = dataArray[index];

                    for (int j = 0; j < 5; j++) {
                        System.out.println(Thread.currentThread().getName() + " array[j] = " + array[j]);
                        if (array[j] == 1) {
                            System.out.println(Thread.currentThread().getName() + " 执行CyclicBarrier抑制");
                            cyclicBarrier.await();
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }

            }).start();
        }
    }

}
